package com.ai.wx.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.core.constant.GlobalConstant;
import com.ai.core.pagemode.Grid;
import com.ai.core.pagemode.Json;
import com.ai.core.pagemode.PageFilter;
import com.ai.wx.entity.AgentBrokerage;
import com.ai.wx.entity.AgentBrokerageDraw;
import com.ai.wx.entity.AgentPromotion;
import com.ai.wx.entity.AgentUser;
import com.ai.wx.pojo.PproductPromotion;
import com.ai.wx.service.AgentService;
import com.ai.wx.service.ProductPromotionService;

@Controller
@RequestMapping("/agent")
public class AgentViewController extends BaseController{

	@Autowired
	private AgentService agentService;
	@Autowired
	private ProductPromotionService productPromotionService;
	
	@Autowired
	private HttpServletRequest request;
	
 
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		/**
		 * 自动转换日期类型的字段格式
		 */
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
	

	@RequestMapping("/agentApproving")
	public String agentApproving() {
		return "/agentApproving";
	}
	@RequestMapping("/agentUser")
	public String agentUser() {
		setJsSign(request);
		//getuth2AccessToken(request);
		AgentUser agentUser=getAgentUser(request);
		if(agentUser == null){
			return "/agentUser";
		}else{
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_APPROVING){
				return "/agentApproving";
			}
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_OK){
				return "/agentUser";
			}
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_REFUSE){
				return "/agentUser";
			}
			return "/agentApproving";
		} 
	}
	@RequestMapping("/brokerage")
	public String brokerage() {
		setJsSign(request);
		//getuth2AccessToken(request); 
		AgentUser agentUser=getAgentUser(request);
		if(agentUser == null){
			return "/agentUser";
		}else{
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_APPROVING){
				return "/agentApproving";
			}
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_REFUSE){
				return "/agentUser";
			}

			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_OK){
				AgentBrokerage agentBrokerage = agentService.getAgentBrokerage(agentUser.getId());
				Long count = agentService.getBrokerageCount(agentUser.getId());
				request.setAttribute("agentBrokerage", agentBrokerage);
				request.setAttribute("agentBrokerageCount", count); 
				return "/brokerage";
			}
			
			return "/agentApproving";
		}  
	}
	
	@RequestMapping("/promotionCodeSetting")
	public String promotionCodeSetting() {
		setJsSign(request);
		//getuth2AccessToken(request); 
		AgentUser agentUser=getAgentUser(request);
		if(agentUser == null){
			return "/agentUser";
		}else{
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_APPROVING){
				return "/agentApproving";
			}
			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_REFUSE){
				return "/agentUser";
			}

			if(agentUser.getAgentState() == GlobalConstant.AGENT_STATE_OK){
				AgentPromotion promotion = agentService.buyAgentPromoiton(agentUser.getId());
				request.setAttribute("promotion", promotion);
				List<PproductPromotion> promoList=productPromotionService.getPromoList();
				request.setAttribute("promoList", promoList);
				return "/promotionCodeSetting";
			}
			
			return "/agentApproving";
		}  
		
	}
	
	@RequestMapping("/doPromotionCodeSetting")
	public Json doPromotionCodeSetting(Integer promotionRate) {
		Json j = new Json();
		try{

			AgentUser agentUser=getAgentUser(request);
			if(agentUser == null){
				j.setMsg("登陆失效请重新打开该");
			}else{ 
				agentService.setAgentPromoiton(agentUser.getId(), promotionRate);
				j.setSuccess(true);
			}
		}catch(Exception e){
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/brokerageList")
	public String brokerageList() {
		setJsSign(request);
		//getuth2AccessToken(request); 
		AgentUser agentUser=getAgentUser(request);
		PageFilter pf = new PageFilter();
		pf.setPage(1);
		pf.setRows(20);
		Grid agentBrokerageDtlList = agentService.getAgentBrokerageDtl(agentUser.getId(), pf);
 		request.setAttribute("agentBrokerageDtlList", agentBrokerageDtlList);
 		return "/brokerageList";
	}
	
	@RequestMapping("/getBrokerageList")
	@ResponseBody
	public Grid getBrokerageList(PageFilter pf) {  
		AgentUser agentUser=getAgentUser(request); 
		Grid agentBrokerageList = agentService.getAgentBrokerageDtl(agentUser.getId(), pf);
 
  		return agentBrokerageList;
	}
	
	
	@RequestMapping("/brokerageDraw")
	public String brokerageDraw() {
		setJsSign(request);
		//getuth2AccessToken(request); 
		AgentUser agentUser=getAgentUser(request);
		AgentBrokerage agentBrokerage = agentService.getAgentBrokerage(agentUser.getId());
 		request.setAttribute("agentBrokerage", agentBrokerage);
		if(agentBrokerage.getLockCharge() > 0){
			return "/brokerageDrawLock";
		}
 		return "/brokerageDraw";
	}
	
	
	@RequestMapping("/brokerageDrawList")
	public String brokerageDrawList() {
		setJsSign(request);
		//getuth2AccessToken(request); 
		AgentUser agentUser=getAgentUser(request);
		PageFilter page = new PageFilter();
		page.setPage(1);
		page.setRows(20);
		Grid brokerageDrawList = agentService.getAgentBrokerageDraw(agentUser.getId(), null, null, null, page);
 		request.setAttribute("agentBrokerageDrawList", brokerageDrawList);
		 
 		return "/brokerageDrawList";
	}
	
	@RequestMapping("/getBrokerageDrawList")
	@ResponseBody
	public Grid getBrokerageDrawList(PageFilter page) {  
		AgentUser agentUser=getAgentUser(request); 
		Grid brokerageDrawList = agentService.getAgentBrokerageDraw(agentUser.getId(), null, null, null, page);
  		return brokerageDrawList;
	}
	
	  

	@RequestMapping("/regAgentUser")
	@ResponseBody
	public Json regAgentUser(AgentUser agentUser) throws IOException {
		Json j = new Json();
		try {
			 if(StringUtils.isEmpty(agentUser.getUserName())){
				j.setMsg("姓名不能为空");
			}else if(StringUtils.isEmpty(agentUser.getIdCard())){
				j.setMsg("身份证号码不能为空");
			}else if(StringUtils.isEmpty(agentUser.getEmail())){
				j.setMsg("邮箱不能为空");
			}else if(StringUtils.isEmpty(agentUser.getPhone())){
				j.setMsg("手机不能为空");
			}else if(StringUtils.isEmpty(agentUser.getBankCard())){
				j.setMsg("银行卡号不能为空");
			}else if(StringUtils.isEmpty(agentUser.getBankName())){
				j.setMsg("开户银行不能为空");
			}else{ 
				Object openidBbj = request.getSession().getAttribute("openid");
				if(openidBbj == null){
					j.setMsg("登陆失效请重新打开申请页面");
				}else{

					String openid = (String)openidBbj;
					agentUser.setOpenid(openid);
					agentService.registerAgentUser(agentUser);
					j.setSuccess(true);
					j.setMsg("注册成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());

		}
		return j;
	}
	
	 
	
	 
	
	@RequestMapping("/getAgentBrokerage")
	@ResponseBody
	public Json getAgentBrokerage(Integer  agentId) throws IOException {
		Json j = new Json();
		try {
			AgentBrokerage account = agentService.getAgentBrokerage(agentId);
			j.setSuccess(true); 
			j.setObj(account);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());

		}
		return j;
	}
	
	
	@RequestMapping("/getAgentBrokerageDtl")
	@ResponseBody
	public Grid getAgentBrokerageDtl(Integer  agentId,PageFilter page) throws IOException {
	 
		Grid g = new Grid(); 
		try {
			 g = agentService.getAgentBrokerageDtl( agentId, page);
 
		} catch (Exception e) {
			e.printStackTrace(); 
			g.setErrorMsg(e.getMessage());

		}
		return g;
	}
	
	
	
	
	@RequestMapping("/getAgentBrokerageDraw")
	@ResponseBody
	public Grid getAgentBrokerageDraw(Integer agentId, Integer drawState, Date startDate, Date endDate,PageFilter page) throws IOException {
	 
		Grid g = new Grid(); 
		try {
			 g = agentService.getAgentBrokerageDraw(agentId, drawState, startDate, endDate, page);
 
		} catch (Exception e) {
			e.printStackTrace(); 
			g.setErrorMsg(e.getMessage());

		}
		return g;
	}

	@RequestMapping("brokerageDrawLock")
	public String brokerageDrawLock(){
		return "/brokerageDrawLock";
	}
	
	 
	
	
	@RequestMapping("/applyDrawBrokerage")
	@ResponseBody
	public Json applyDrawBrokerage(AgentBrokerageDraw draw) throws IOException {
		Json j = new Json();
		try {
			AgentUser agentUser=getAgentUser(request); 
			if(agentUser == null){
				j.setMsg("登陆失效请重新打开申请页面");
			}else{
				draw.setAgentId(agentUser.getId());
			    j = agentService.applyDrawBrokerage(draw); 
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());

		}
		return j;
	}
	
  
}
